﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ArduinoSketch.Core
{
    public partial class NewFileDialog : Form
    {
        private NewFileType _newFileType;

        public NewFileDialog()
        {
            InitializeComponent();
        }

        public NewFileDialog(NewFileType newFileType, string projectName):this()
        {
            _newFileType = newFileType;
            switch (_newFileType)
            {
                case NewFileType.HEADER_FILE:
                    this.Text = "New Source File";
                    lbTitle.Text = "Create new header file";
                    break;
                case NewFileType.SOURCE_FILE:
                    this.Text = "New Header File";
                    lbTitle.Text = "Create new source file";
                    break;
            }
            this.lbProjectName.Text = projectName;
        }

        private bool CheckFileExtension(string text)
        {
            bool _result = false;

            switch (_newFileType)
            {
                case NewFileType.HEADER_FILE:
                    _result = text.IndexOf(".h") != -1;
                    break;
                case NewFileType.SOURCE_FILE:
                    _result = text.IndexOf(".cpp") != -1;
                    break;
            }
            return _result;
        }

        public string NewFileName { get { return this.txtFileName.Text; } }

        private void txtFileName_TextChanged(object sender, EventArgs e)
        {
            btnFinish.Enabled = false;
            if (String.IsNullOrEmpty(txtFileName.Text))
            {
                lbTitle.Image = ArduinoSketch.Core.Properties.Resources.error;
                lbTitle.Text = "       File name is empty";
            }
            else if (!CheckFileExtension(txtFileName.Text))
            {
                lbTitle.Image = ArduinoSketch.Core.Properties.Resources.warning;
                switch (_newFileType)
                {
                    case NewFileType.HEADER_FILE:
                        lbTitle.Text = "       File name is discouraged. File extension dose not correspond to known header file types";
                        break;
                    case NewFileType.SOURCE_FILE:
                        lbTitle.Text = "       File name is discouraged. File extension dose not correspond to known source file types";
                        break;
                }
            }
            else
            {
                lbTitle.Image = null;
                btnFinish.Enabled = true;
                lbTitle.Text = _newFileType == NewFileType.HEADER_FILE ? "Create new header file" : "Create new source file";
            }
        }

        private void btnFinish_Click(object sender, EventArgs e)
        {

        }
    }
}
